﻿using Dapper;
using System;
using BCEntity.TMS.OrderFreightAppendRecord;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.TMS.OrderFreightAppendRecord
{
	/// <summary>
	/// 订单运费追加记录
	/// </summary>
	public class OrderFreightAppendRecordData : IOrderFreightAppendRecordData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM orderfreightappendrecord";
		private const string QUERY = @"SELECT OrderFreightAppendRecordId,OrderId,OrderNo,SecendWeighingDifference,PaymentRecordId,CreateTime,PaidTime FROM orderfreightappendrecord";
		public OrderFreightAppendRecordData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查订单运费追加记录是否存在
		public async Task<bool> Exists(long orderFreightAppendRecordId)
		{
			string sql = string.Format(@"{0} WHERE OrderFreightAppendRecordId=?OrderFreightAppendRecordId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderFreightAppendRecordId", orderFreightAppendRecordId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加订单运费追加记录
		public async Task<OrderFreightAppendRecordEntity> Add(OrderFreightAppendRecordEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("orderfreightappendrecord", new string[] { "OrderId", "OrderNo", "SecendWeighingDifference", "PaymentRecordId" });
			sql = string.Format("{0} {1} WHERE OrderFreightAppendRecordId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderId", entity.OrderId, DbType.Int64, size: 20);
			parameters.Add("?OrderNo", entity.OrderNo?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?SecendWeighingDifference", entity.SecendWeighingDifference, DbType.Decimal, size: 10);
			parameters.Add("?PaymentRecordId", entity.PaymentRecordId, DbType.Int64, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderFreightAppendRecordEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改订单运费追加记录
		public async Task<OrderFreightAppendRecordEntity> Update(OrderFreightAppendRecordEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("orderfreightappendrecord", new string[] { "PaidTime" },
				new string[] { "OrderFreightAppendRecordId" });
			sql = string.Format("{0} {1} WHERE OrderFreightAppendRecordId=?OrderFreightAppendRecordId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderFreightAppendRecordId", entity.OrderFreightAppendRecordId, DbType.Int64, size: 20);
			parameters.Add("?PaidTime", entity.PaidTime, DbType.DateTime);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderFreightAppendRecordEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除订单运费追加记录
		public async Task<bool> Delete(long orderFreightAppendRecordId)
		{
			string sql = @"DELETE FROM orderfreightappendrecord WHERE OrderFreightAppendRecordId=?OrderFreightAppendRecordId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderFreightAppendRecordId", orderFreightAppendRecordId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取订单运费追加记录
		public async Task<OrderFreightAppendRecordEntity> GetEntity(long orderFreightAppendRecordId)
		{
			string sql = string.Format(@"{0} WHERE OrderFreightAppendRecordId=?OrderFreightAppendRecordId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OrderFreightAppendRecordId", orderFreightAppendRecordId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<OrderFreightAppendRecordEntity>(sql, parameters);
		}
		#endregion

		#region 获取订单运费追加记录列表
		public async Task<IEnumerable<OrderFreightAppendRecordEntity>> GetList()
		{
			return await this.databaseContext.Master.QueryAsync<OrderFreightAppendRecordEntity>(QUERY);
		}
		#endregion

		#region 获取订单运费追加记录分页
		public async Task<(IEnumerable<OrderFreightAppendRecordEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM orderfreightappendrecord {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<OrderFreightAppendRecordEntity, int>(sql, parameters);
		}
		#endregion
	}
}



